﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MSBTC.WinForms;
using MSBTC.Model;
using MSBTC.WinFormHelpers;

namespace MSBTC
{
    public partial class MainForm : Form
    {
        MDIParent mdipar = new MDIParent();
        PMSContext dbcontext = new PMSContext();
        FormHelpers helper = new FormHelpers();
        SessionHelper seshelper = new SessionHelper();

        public MainForm()
        {
            InitializeComponent();
            
        }

        public static Form IsFormAlreadyOpen(Type FormType)
        {
            foreach (Form OpenForm in Application.OpenForms)
            {
                if (OpenForm.GetType() == FormType)
                    return OpenForm;
            }

            return null;
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            string date = DateTime.Today.ToShortDateString();
            string ip = seshelper.Session_GetIP();

            var ses = (from sess in dbcontext.Sessions
                       where sess.Role_Name == "Administrator" && sess.Date_Created == date
                       && sess.IPAddress == ip && sess.Date_Logout == null
                       select sess).SingleOrDefault();
            if (ses != null)
            {
                pbUser.Enabled = true;
                /*
                 Administrator
                Engineer
                Foreman
                Warehouse Manager
                 */
            }
                var ses1 = (from sess in dbcontext.Sessions
                       where sess.Role_Name == "Engineer" && sess.Date_Created == date
                       && sess.IPAddress == ip && sess.Date_Logout == null
                       select sess).SingleOrDefault();
                if (ses1 != null)
                {
                    
                        pbUser.Enabled = false;
                        pbClient.Enabled = false;
                        pbSupplier.Enabled = false;
                        pbLabor.Enabled = false;
                    
                }

                var ses2 = (from sess in dbcontext.Sessions
                    where sess.Role_Name == "Foreman" && sess.Date_Created == date
                    && sess.IPAddress == ip && sess.Date_Logout == null
                    select sess).SingleOrDefault();
                if (ses2 != null)
                {   
                        pbUser.Enabled = false;
                        pbClient.Enabled = false;
                        pbMonitoring.Enabled = false;
                        pbSupplier.Enabled = false;
                        pbLabor.Enabled = false;   
                }

                var ses3 = (from sess in dbcontext.Sessions
                            where sess.Role_Name == "Warehouse Manager" && sess.Date_Created == date
                            && sess.IPAddress == ip && sess.Date_Logout == null
                            select sess).SingleOrDefault();
                if (ses3 != null)
                {
                    pbUser.Enabled = false;
                    pbClient.Enabled = false;
                    pbMonitoring.Enabled = false;
                    pbLabor.Enabled = false;
                    pbPO.Enabled = false;
                    pbDR.Enabled = false;

                }
                
            lblWelcm.Text = "Welcome " + helper.GetName();
    
        }

        #region Hover
        private void pbDR_MouseHover(object sender, EventArgs e)
        {
            pbDR.Cursor = Cursors.Hand;
        }

        private void pbMaterials_MouseHover(object sender, EventArgs e)
        {
            pbMaterials.Cursor = Cursors.Hand;
        }

        private void pbEquip_MouseHover(object sender, EventArgs e)
        {
            pbEquip.Cursor = Cursors.Hand;
        }

        private void pbSupplier_MouseHover(object sender, EventArgs e)
        {
            pbSupplier.Cursor = Cursors.Hand;
        }

        private void pbClient_MouseHover(object sender, EventArgs e)
        {
            pbClient.Cursor = Cursors.Hand;
        }

        private void pbLabor_MouseHover(object sender, EventArgs e)
        {
            pbLabor.Cursor = Cursors.Hand;
        }

        private void pbMonitoring_MouseHover(object sender, EventArgs e)
        {
            pbMonitoring.Cursor = Cursors.Hand;
        }

        private void pbPjInfo_MouseHover(object sender, EventArgs e)
        {
            pbPjInfo.Cursor = Cursors.Hand;
        }

        private void pbPO_MouseHover(object sender, EventArgs e)
        {
            pbPO.Cursor = Cursors.Hand;
        }

        private void pbUser_MouseHover(object sender, EventArgs e)
        {
            pbUser.Cursor = Cursors.Hand;
        }
        //private void LLCat_MouseHover(object sender, EventArgs e)
        //{
        //    LLCat.Cursor = Cursors.Hand;
        //}

        #endregion

        #region mouse click
        private void pbClient_Click(object sender, EventArgs e)
        {
            ClientForm CForm = null;

            if ((CForm == IsFormAlreadyOpen(typeof(ClientForm))) && (CForm == null))
            {
                CForm = new ClientForm();
                CForm.Show();
            }
            else
            {
                CForm.Close();
            }

        }

        private void pbSupplier_Click(object sender, EventArgs e)
        {
            SupplierForm SForm = new SupplierForm();
            SForm.Show();
        }

        private void pbEquip_Click(object sender, EventArgs e)
        {
            EquipForm EForm = new EquipForm();
            EForm.Show();
        }

        private void pbUser_Click(object sender, EventArgs e)
        {
            UserForm UForm = new UserForm();
            UForm.Show();
        }

        private void pbDR_Click(object sender, EventArgs e)
        {
            DeliveryReceiptForm DRForm = new DeliveryReceiptForm();
            DRForm.Show();

        }

        private void LLCat_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            CategoryForm CatForm = new CategoryForm();
            CatForm.Show();
        }

        private void LLLogout_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            string ip = seshelper.Session_GetIP();
            string date = DateTime.Today.ToShortDateString();
            var log = (from ses in dbcontext.Sessions
                       where ses.Date_Logout == null && ses.Date_Created == date
                       && ses.IPAddress == ip
                       select ses).SingleOrDefault();

            if (log != null)
            {
                log.Date_Logout = date;

                dbcontext.Entry(log).State = EntityState.Modified;
                dbcontext.SaveChanges();

                //Login Lform = new Login();
                //MainForm Mform = new MainForm();
                //Mform.Enabled = false;
                //mdipar.Enabled = false;
                //Lform.Show();
                Application.Restart();
                
            }
        }

        private void pbLabor_Click(object sender, EventArgs e)
        {
            LabourForm LbForm = new LabourForm();
            LbForm.Show();
        }


        private void pbPjInfo_Click_1(object sender, EventArgs e)
        {
            ProjectInfoForm PIForm = new ProjectInfoForm();
            PIForm.Show();
        }

        private void pbDamage_Click(object sender, EventArgs e)
        {
            POForm DForm = new POForm();
            DForm.Show();
        }

        private void pbMaterials_Click(object sender, EventArgs e)
        {
            MaterialsInventoryForm Matinv = new MaterialsInventoryForm();
            Matinv.Show();
        }

        private void pbMonitoring_Click(object sender, EventArgs e)
        {
            PMSForm PMSForm = new PMSForm();
            PMSForm.Show();
        }

        #endregion

        private void LLLost_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            LostItemForm Ltform = new LostItemForm();
            Ltform.Show();
        }

        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            MaterialEstimationForm Meform = new MaterialEstimationForm();
            Meform.Show();
        }

        private void lblRequest_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            RequisitionForm rform = new RequisitionForm();

            rform.Show();
        }

        private void linkLabel2_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            //Mat_Maintenance MatMform = new Mat_Maintenance();
            //MatMform.Show();
        }
    }
}
